namespace SimpleWebServices.Configuration
{
    using System;
    using System.IO;
    using System.Web;
    using System.Xml;
    using System.Xml.Serialization;
    using SimpleWebServices.Web;

    [XmlRoot("file")]
    public sealed class FileSetting
    {
        [XmlAttribute("cache-condition")]
        public string CacheCondition
        {
            get;
            set;
        }

        [XmlAttribute("cache-control")]
        public string CacheControl
        {
            get;
            set;
        }

        [XmlAttribute("content-type")]
        public string ContentType
        {
            get;
            set;
        }

        [XmlIgnore]
        public TimeSpan Duration
        {
            get
            {
                return string.IsNullOrEmpty(this.DurationValue) ? new TimeSpan(0) : XmlConvert.ToTimeSpan(this.DurationValue);
            }
        }

        [XmlAttribute("duration")]
        public string DurationValue
        {
            get;
            set;
        }

        [XmlAttribute("location")]
        public string Location
        {
            get;
            set;
        }

        [XmlAttribute("path")]
        public string Path
        {
            get;
            set;
        }

        [XmlIgnore]
        public HttpCacheability Cacheability
        {
            get
            {
                HttpCacheability value = HttpCacheability.NoCache;

                if ("private".Equals(this.CacheControl, StringComparison.OrdinalIgnoreCase))
                {
                    value = HttpCacheability.Private;
                }
                else if ("public".Equals(this.CacheControl, StringComparison.OrdinalIgnoreCase))
                {
                    value = HttpCacheability.Public;
                }
                else if ("server".Equals(this.CacheControl, StringComparison.OrdinalIgnoreCase))
                {
                    value = HttpCacheability.Server;
                }

                return value;
            }
        }

        [XmlIgnore]
        public FileInfo File
        {
            get
            {
                string fileName = null;
                if (string.IsNullOrEmpty(this.Location))
                {
                }
                else if (this.Location.StartsWith("\\", StringComparison.OrdinalIgnoreCase))
                {
                    /// UNC path
                    fileName = this.Location;
                }
                else if (this.Location.Substring(1, 2).Equals(@":\", StringComparison.OrdinalIgnoreCase))
                {
                    /// Drive path
                    fileName = this.Location;
                }
                else if (this.Location.StartsWith("~", StringComparison.OrdinalIgnoreCase))
                {
                    /// Virtual path
                    fileName = HttpContextFactory.Current.Request.MapPath(this.Location);
                }
                else
                {
                    /// App relative path
                    fileName = System.IO.Path.Combine(HttpContextFactory.Current.Request.PhysicalApplicationPath, this.Location);
                }

                FileInfo result = null;

                if (null != fileName)
                {
                    result = new FileInfo(fileName);
                }

                return result;
            }
        }
    }
}